package practise.alogrithm.hundredInterviewQuestions;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Find out the least K numbers in a array.
 */
public class TopK {
    List<Integer> arrays =  new ArrayList<>();
    PriorityQueue<Integer> topKQueue ;
    int size;
    public TopK(List<Integer> ints, int k){
        this.arrays = ints;
        topKQueue = new PriorityQueue<>(k);

        size=k;
        addAll(ints);
    }

    public TopK(int k){
        topKQueue = new PriorityQueue<>(k);

        size=k;
    }

    public void add(Integer i){
        arrays.add(i);
        addInMinStack(i);
    }

    public void addAll(List<Integer> ints){
        for(Integer value : ints){
            arrays.add(value);
            topKQueue.add(value);
        }

    }
    private void addInMinStack(Integer i){
        if(topKQueue.size()<size){
            topKQueue.offer(i);
        }
        else{
            topKQueue.offer(Math.min(i,topKQueue.poll()));
        }
    }


    private void addInMinStack(Integer i,PriorityQueue<Integer> minTop, int size){
        if(minTop.size()<size){
            minTop.offer(i);
        }
        else{
            minTop.offer(Math.min(i,minTop.poll()));
        }
    }

    public List<Integer> getTopK(int[] values, int k){
        PriorityQueue<Integer> minTop = new PriorityQueue<Integer>(k);
        for(int i : values){
            addInMinStack(i,minTop,k);
        }
        return null;
    }
}
